package br.com.fiap.mercadinho.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import br.com.fiap.mercadinho.common.GenericDAOException;

public class GenericDAO {

	protected Connection conn;
	protected Integer lastInsertId;

	public Integer getLastInsertId() {
		return lastInsertId;
	}

	public void setLastInsertId(Integer lastInsertId) {
		this.lastInsertId = lastInsertId;
	}

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void openConnection() throws GenericDAOException {
		try {
			if (conn == null || conn.isClosed()) {
				conn = DriverManager
						.getConnection("jdbc:mysql://localhost:3306/mercadinhoweb?user=root&password=fiap");
			}
		} catch (SQLException e) {
			throw new GenericDAOException("Open Connection error: "
					+ e.getMessage());
		}
	
	}
	
	protected void closeConnction() throws GenericDAOException{
		try {
			conn.close();
		} catch (SQLException e) {
			throw new GenericDAOException("Close Connection error: "
					+ e.getMessage());
		}
	}
}
